package com.ruoyi.project.gxgw_modules.machine.service;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.gxgw_modules.common.DelFlag;
import com.ruoyi.project.gxgw_modules.machine.domain.Machine;
import com.ruoyi.project.gxgw_modules.machine.domain.MachineMap;
import com.ruoyi.project.gxgw_modules.machine.mapper.MachineMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.support.Convert;

/**
 * 设备 服务层实现
 * 
 * @author ruoyi
 * @date 2019-01-17
 */
@Service
public class MachineServiceImpl implements IMachineService 
{
	@Autowired
	private MachineMapper machineMapper;

	/**
     * 查询设备信息
     * 
     * @param macId 设备ID
     * @return 设备信息
     */
    @Override
	public Machine selectMachineById(Integer macId)
	{
	    return machineMapper.selectMachineById(macId);
	}
	
	/**
     * 查询设备列表
     * 
     * @param machine 设备信息
     * @return 设备集合
     */
	@Override
	public List<Machine> selectMachineList(Machine machine)
	{
	    return machineMapper.selectMachineList(machine);
	}
	
    /**
     * 新增设备
     * 
     * @param machine 设备信息
     * @return 结果
     */
	@Override
	public int insertMachine(Machine machine)
	{
		Date now = new Date();
		Long userId = ShiroUtils.getUserId();
		machine.setCreateBy(userId.toString());
		machine.setCreateDate(now);
		machine.setDelFlag(DelFlag.ALIVE.getCode());
		machine.setUpdateBy(userId.toString());
		machine.setUpdateDate(now);
		machine.setOrder((int) Math.random());
	    return machineMapper.insertMachine(machine);
	}
	
	/**
     * 修改设备
     * 
     * @param machine 设备信息
     * @return 结果
     */
	@Override
	public int updateMachine(Machine machine)
	{
		Date now = new Date();
		Long userId = ShiroUtils.getUserId();
		machine.setUpdateBy(userId.toString());
		machine.setUpdateDate(now);
		// TO DO order字段是否有必要？
		// machine.setOrder((int) Math.random());
	    return machineMapper.updateMachine(machine);
	}

	/**
     * 删除设备对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteMachineByIds(String ids)
	{
		return machineMapper.deleteMachineByIds(Convert.toStrArray(ids));
	}

	@Override
	public List<MachineMap> selectMap() {
		return machineMapper.selectMap();
	}

}
